package com.xbongbong.pro.rabbitmq.producer.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.MessageErrorCodeEnum;
import com.xbongbong.pro.enums.smartsettings.enums.AiAssistantPushTypeEnum;
import com.xbongbong.pro.message.pojo.dto.MessageRabbitMqDTO;
import com.xbongbong.pro.rabbitmq.binders.AiAssistantProducerBinder;
import com.xbongbong.pro.rabbitmq.producer.AiAssistantBaseProducer;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Author: hongxiao
 * @date: 2019-11-17 11:05
 */
@Service("aiAssistantBaseProducer")
@EnableBinding({AiAssistantProducerBinder.class})
public class AiAssistantBaseProducerImpl implements AiAssistantBaseProducer {
    private static final String HEADER_NAME = "type";

    @Resource
    private AiAssistantProducerBinder aiAssistantProducerBinder;

    @Override
    public void sendMessage(AiAssistantPushTypeEnum pushTypeEnum, MessageRabbitMqDTO message) throws XbbException {
        MessageBuilder messageBuilder;
        MessageHeaderAccessor messageHeaderAccessor = new MessageHeaderAccessor();
        Object header;
        if (Objects.nonNull(pushTypeEnum) && Objects.nonNull(message)) {
            header = pushTypeEnum;
            messageHeaderAccessor.setHeader(HEADER_NAME, header);
            messageBuilder = MessageBuilder.withPayload(message).setHeaders(messageHeaderAccessor);
            switch (pushTypeEnum) {
                case CUSTOMER_PUSH:
                    aiAssistantProducerBinder.customerOutput().send(messageBuilder.build());
                    break;
                case CONTACT_PUSH:
                    aiAssistantProducerBinder.contactOutput().send(messageBuilder.build());
                    break;
                case OPPORTUNITY_PUSH:
                    aiAssistantProducerBinder.opportunityOutput().send(messageBuilder.build());
                    break;
                case CONTRACT_PUSH:
                    aiAssistantProducerBinder.contractOutput().send(messageBuilder.build());
                    break;
                case REFUND_PUSH:
                    aiAssistantProducerBinder.refundOutput().send(messageBuilder.build());
                    break;
                case WORK_REPORT_PUSH:
                    aiAssistantProducerBinder.workReportOutput().send(messageBuilder.build());
                    break;
                case QUOTATION_PUSH:
                    aiAssistantProducerBinder.quotationOutput().send(messageBuilder.build());
                    break;

                // 应收款
                case PAYMENT_PLAN_PUSH:
                    aiAssistantProducerBinder.paymentOutput().send(messageBuilder.build());
                    break;
                case INVOICE_PUSH:
                    aiAssistantProducerBinder.invoiceOutput().send(messageBuilder.build());
                    break;
                case PAY_PLAN_PUSH:
                    aiAssistantProducerBinder.payPlanOutput().send(messageBuilder.build());
                    break;


                case SUPPLIER_PUSH:
                    aiAssistantProducerBinder.supplierOutput().send(messageBuilder.build());
                    break;
                case SUPPLIER_CONTACT_PUSH:
                    aiAssistantProducerBinder.supplierContactOutput().send(messageBuilder.build());
                    break;
                case PURCHASE_PUSH:
                    aiAssistantProducerBinder.purchaseOutput().send(messageBuilder.build());
                    break;
                case RETURNED_PURCHASE_PUSH:
                    aiAssistantProducerBinder.returnedPurchaseOutput().send(messageBuilder.build());
                break;

                case INSTOCK_PUSH:
                    aiAssistantProducerBinder.instockOutput().send(messageBuilder.build());
                    break;
                case OUTSTOCK_PUSH:
                    aiAssistantProducerBinder.outstockOutput().send(messageBuilder.build());
                    break;
                case ASSEMBLE_PUSH:
                    aiAssistantProducerBinder.assembleOutput().send(messageBuilder.build());
                    break;
                case TRANSFER_PUSH:
                    aiAssistantProducerBinder.transferOutput().send(messageBuilder.build());
                    break;
                case INVENTORY_PUSH:
                    aiAssistantProducerBinder.inventoryOutput().send(messageBuilder.build());
                    break;

                case BOMBILL_PUSH:
                    aiAssistantProducerBinder.bomOutput().send(messageBuilder.build());
                    break;
                case PRODUCTION_ORDER_PUSH:
                    aiAssistantProducerBinder.productionOrderOutput().send(messageBuilder.build());
                    break;
                /**
                case WORKORDER_PUSH:
                    header = PushTypeEnum.WORKORDER_PUSH;
                    messageHeaderAccessor.setHeader(HEADER_NAME, header);
                    messageBuilder = MessageBuilder.withPayload(message).setHeaders(messageHeaderAccessor);
                    producerBinder.workOrderOutput().send(messageBuilder.build());
                    break;
                 */

                case PRODUCT_PUSH:
                    aiAssistantProducerBinder.productOutput().send(messageBuilder.build());
                    break;
                case PRODUCT_STOCK_PUSH:
                    aiAssistantProducerBinder.productStockOutput().send(messageBuilder.build());
                    break;
                case PRODUCT_STOCK_WARNING_PUSH:
                    aiAssistantProducerBinder.productStockWarningOutput().send(messageBuilder.build());
                    break;
                case PRODUCT_STOCK_GUARANTEE_PUSH:
                    aiAssistantProducerBinder.productStockGuaranteeOutput().send(messageBuilder.build());
                    break;


                case WAIT_PURCHASE_PUSH:
                    aiAssistantProducerBinder.waitPurchaseOutput().send(messageBuilder.build());
                    break;
                case WAIT_PRODUCE_PUSH:
                    aiAssistantProducerBinder.waitProduceOutput().send(messageBuilder.build());
                    break;
                case WAIT_OUTSTOCK_PUSH:
                    aiAssistantProducerBinder.waitOutstockOutput().send(messageBuilder.build());
                    break;
                case WAIT_INSTOCK_PUSH:
                    aiAssistantProducerBinder.waitInstockOutput().send(messageBuilder.build());
                    break;
                //市场管理(市场活动 销售线索)
                case MARKET_ACTIVITY_PUSH:
                    aiAssistantProducerBinder.marketActivityOutput().send(messageBuilder.build());
                    break;
                case CLUE_PUSH:
                    aiAssistantProducerBinder.clueOutput().send(messageBuilder.build());
                    break;
                case PAAS_PUSH:
                    aiAssistantProducerBinder.paasOutput().send(messageBuilder.build());
                    break;
                case UNKNOWN:
                    throw new XbbException(MessageErrorCodeEnum.API_ERROR_380001);
                default:
                    break;
            }
        }
    }
}
